package com.libl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

public class ShiroDemo {
    public static void main(String[] args) {
        //创建SecurityManager核心对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //获取配置文件（临时充当数据库角色）中的权限数据，里面有用户、角色、权限
        IniRealm iniRealm = new IniRealm("classpath:realm.ini");
        //将权限数据放入核心对象中
        securityManager.setRealm(iniRealm);

        //设置SecurityManagerUtils工具类
        SecurityUtils.setSecurityManager(securityManager);

        //得到Subject主体对象
        Subject subject = SecurityUtils.getSubject();

        //封装AuthenticationToken，用户输入的用户名和密码
        AuthenticationToken token = new UsernamePasswordToken("zhangsan", "123456");

        //登录
        subject.login(token);

        //测试是否成功
        if (subject.isAuthenticated()){
            System.out.println("登陆成功");
        }
    }
}
